package helloworld

import (
	"context"
	"fmt"
	"mychat/internal/common/errors"
	"mychat/internal/common/logger"
	"mychat/internal/conf"
	"mychat/internal/data"
)

type HelloWorldService interface {
	Ping(ctx context.Context) (*PingResp, *errors.Error)
	Hi(ctx context.Context, name string) (*HelloResp, *errors.Error)
	Hello(ctx context.Context, req *HelloRequest) (*HelloResp, *errors.Error)
}

type helloWorldService struct {
	bc   *conf.Bootstrap
	data *data.Data
	lg   logger.Logger
}

func NewHelloWorldServie(bc *conf.Bootstrap, lg logger.Logger, data *data.Data) HelloWorldService {
	return &helloWorldService{
		bc:   bc,
		data: data,
		lg:   lg.WithCustomStrTag("hellowworld_service"),
	}
}

// Ping
// @Summary ping test
// @Description
// @Tags helloworld
// @Accept mpfd
// @Produce json
// @Success 200 {object} PingResp
// @Failure 400 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Router /ping [get]
func (s *helloWorldService) Ping(ctx context.Context) (*PingResp, *errors.Error) {
	ret := "pong"
	return &PingResp{Ret: ret}, nil
}

// Hello
// @Summary hello world
// @Description
// @Tags helloworld
// @Accept x-www-form-urlencoded
// @Param name query string true "person name"
// @Produce json
// @Success 200 {object} HelloResp
// @Failure 400 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Router /hello [get]
func (s *helloWorldService) Hello(ctx context.Context, req *HelloRequest) (*HelloResp, *errors.Error) {
	if len(req.Name) == 0 {
		return nil, &errors.ParamIsRequired
	}

	return &HelloResp{Ret: fmt.Sprintf("Hello, %s!", req.Name)}, nil
}

// Hi
// @Summary hi world
// @Description
// @Tags helloworld
// @Accept x-www-form-urlencoded
// @Param name path string true "person name"
// @Produce json
// @Success 200 {object} HelloResp
// @Failure 400 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Router /hi/:name [get]
func (s *helloWorldService) Hi(ctx context.Context, name string) (*HelloResp, *errors.Error) {
	if name == "" {
		return nil, &errors.ParamIsRequired
	}

	return &HelloResp{Ret: fmt.Sprintf("Hi, %s!", name)}, nil
}
